package edu.mobbuzz.web.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Retain;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import edu.mobbuzz.daf.bean.Logs;
import edu.mobbuzz.daf.bean.User;
import edu.mobbuzz.daf.dao.ILogsDao;
import edu.mobbuzz.web.base.SecureBasePage;
import edu.mobbuzz.web.facade.ManagementFacade;

public class LogsGridPage extends SecureBasePage{
	private ManagementFacade managementFacade;
	
	private List<Logs> logsList;
	@Property
	private Logs logs;
	
	@Retain
	private BeanModel logsModel;
	@Inject
	private BeanModelSource beanModelSource;
	@Inject
	private ComponentResources resources;
	@Component
	private Form logsForm;
	
	private boolean delete;
	private List<String> deletedList;
	
	public boolean isDelete() {
		return delete;
	}
	
	public void setDelete(boolean delete) {
		if(delete){
			getDeletedList().add(logs.getUrl());
		}
		this.delete = delete;
	}	
	
	public List<String> getDeletedList() {
		if(deletedList == null){
			deletedList = new ArrayList<String>();
		}
		return deletedList;
	}
	
	
	
	public ManagementFacade getManagementFacade() {
		if(managementFacade==null)
			managementFacade = new ManagementFacade();
		return managementFacade;
	}

	public BeanModel getLogsModel() {
		return logsModel;
	}	
	
	public List<Logs> getLogsList() {
		if(logsList==null){
			logsList = new ArrayList<Logs>();
		}
		logsList = getManagementFacade().getLogsList("", "");		
		return logsList;
	}

	public void setLogsList(List<Logs> logsList) {
		this.logsList = logsList;
	}

	void pageLoaded(){
		logsModel = beanModelSource.createDisplayModel(Logs.class, resources.getMessages());
		logsModel.add("actions",null).label("actions");
	}
	
	Object onSuccessFromLogsForm(){		
		for(String element : getDeletedList()){
			logs = (Logs)getManagementFacade().getLogsByPrimaryKey(element);	
			if(logs!=null)
				getManagementFacade().removeLogs(logs);
		}		
		return LogsGridPage.class;
	}
}
